package php.code;

import com.google.common.base.Joiner;
import php.code.indentation.Indentable;
import php.code.indentation.Indentation;
import php.code.instructions.Instruction;

public abstract class Block extends Indentable {
    public final Sequence content = new Sequence(this);

    public Block(Indentable parent) {
        super(parent);
    }

    public Block(Indentation indentation, String tab) {
        super(indentation, tab);
    }

    public Block(Indentation indentation) {
        this(indentation, Indentation.DEFAULT);
    }

    @Override
    public String toString() {
        return Joiner.on("\n").join(
                indent(open()),
                content,
                indent(close())
        );
    }

    protected abstract String open();

    protected abstract String close();

    private String indent(String line) {
        return getIndentation() + line;
    }

    public void add(Instruction element) {
        content.add(element);
        element.setParent(this);
    }
}
